Page({
    data: {
      username: '',
      phone: '',
      password: '',
      confirmPassword: '',
      realName: '',
      sex: '', // 0: 女, 1: 男
      age: '',
      idCard: '',
      address: '',
      emergencyContact: '',
      emergencyTel: ''
    },
  
    // 监听输入事件
    onInput(e) {
      const { field } = e.currentTarget.dataset;
      this.setData({ [field]: e.detail.value });
    },
  
    // 监听性别选择
    onSexChange(e) {
      this.setData({ sex: e.detail.value });
    },
  
    async onRegister() {
      if (!this.data.username || !this.data.phone || !this.data.password || !this.data.confirmPassword) {
        wx.showToast({ title: '请填写必填信息', icon: 'none' });
        return;
      }
      if (this.data.password.length < 6 || this.data.password.length > 20) {
        wx.showToast({ title: '密码长度应在6-20个字符', icon: 'none' });
        return;
      }
      if (this.data.password !== this.data.confirmPassword) {
        wx.showToast({ title: '两次密码不一致', icon: 'none' });
        return;
      }
  
      try {
        await wx.request({
          url: 'http://localhost:8081parent/register',
          method: 'POST',
          data: {
            username: this.data.username,
            password: this.data.password,
            tel: this.data.phone,
            realName: this.data.realName,
            sex: this.data.sex ? parseInt(this.data.sex) : null,
            age: this.data.age ? parseInt(this.data.age) : null,
            idCard: this.data.idCard,
            address: this.data.address,
            emergencyContact: this.data.emergencyContact,
            emergencyTel: this.data.emergencyTel
          },
          success: (res) => {
            if (res.statusCode === 200) {
              wx.showToast({ title: '注册成功', icon: 'success' });
              wx.redirectTo({ url: '/pages/login/login' });
            } else {
              wx.showToast({ title: res.data.message || '注册失败', icon: 'none' });
            }
          }
        });
      } catch (error) {
        console.error('注册失败:', error);
        wx.showToast({ title: '网络错误，请稍后再试', icon: 'none' });
      }
    },
  
    onLogin() {
      wx.navigateTo({ url: '/pages/login/login' });
    }
  });
  